<script setup lang="ts">
import { reactive, ref } from 'vue'
import { login } from '@/request/api'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
interface LoginFormType {
    userName: string
    password: string
}
const formRef = ref<FormInstance>()
const loginForm = reactive<LoginFormType>({
    userName: '',
    password: '',
})
const router = useRouter()

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            login({
                username: loginForm.userName,
                password: loginForm.password,
            }).then((res: any) => {
                if (res.code === 200) {
                    console.log(res)
                    localStorage.setItem('token', res.data.token)
                    ElMessage.success('登录成功')
                    router.push({ name: 'home' })
                } 
            }).catch((err) => {
                console.log(err)
                ElMessage.error(err.msg || '登录失败')
            })
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const tableSchema: { prop: string; label: string }[] = [
    {
        prop: 'userName',
        label: '账号:',
    },
    {
        prop: 'password',
        label: '密码:',
    },
]

const loginRules = reactive<FormRules<typeof loginForm>>(
    {
        userName: [{ required: true, message: '请输入账号', trigger: 'blur' }, { min: 5, max: 20, message: '账号长度在 5 到 20 个字符', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 5, max: 20, message: '密码长度在 5 到 20 个字符', trigger: 'blur' }],
    }
)   
</script>

<template>
    <div class="bg-login">
        <el-form ref="formRef" style="max-width: 600px" :model="loginForm" label-width="auto" class="login-form"
            :rules="loginRules">
            <h2 class="text-center">登录</h2>
            <el-form-item v-for="item in tableSchema" :key="item.prop" :prop="item.prop" :label="item.label">
                <el-input v-model="loginForm[item.prop as keyof typeof loginForm]" style="width: 250px" />
            </el-form-item>
            <div class="btn-center">
                <el-button size="default" type="primary" @click="submitForm(formRef)">登录</el-button>
                <el-button size="default" @click="resetForm(formRef)">重置</el-button>
            </div>
        </el-form>
    </div>
</template>

<style lang="scss" scoped>
.bg-login {
    height: 100%;
    background-image: url('../assets/bg-login.png');
    background-size: cover;
    background-position: center;
    display: flex;
    justify-content: center;
    align-items: center;

    .login-form {
        background-color: rgba(255, 255, 255, 0.8);
        padding: 80px;
        padding-top: 40px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        border-radius: 10px;

        .text-center {
            text-align: center;
            margin-bottom: 20px;
        }

        .btn-center {
            text-align: center;
        }
    }
}
</style>
